/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

package org.apache.directory.http.client.ui.renderer;


import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;


/**
 * TODO AttributeTableCellRenderer.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class AttributeTableCellRenderer extends DefaultTableCellRenderer
{

    protected static Border noFocusBorder = new EmptyBorder( 1, 1, 1, 1 );
    private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder( 1, 1, 1, 1 );

    private Color unselectedForeground;
    private Color unselectedBackground;


    /**
     * Overrides <code>JComponent.setForeground</code> to assign
     * the unselected-foreground color to the specified color.
     * 
     * @param c set the foreground color to this value
     */
    public void setForeground( Color c )
    {
        super.setForeground( c );
        unselectedForeground = c;
    }


    /**
     * Overrides <code>JComponent.setBackground</code> to assign
     * the unselected-background color to the specified color.
     *
     * @param c set the background color to this value
     */
    public void setBackground( Color c )
    {
        super.setBackground( c );
        unselectedBackground = c;
    }


    @Override
    public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column )
    {
        if ( isSelected )
        {
            super.setForeground( table.getSelectionForeground() );
            super.setBackground( table.getSelectionBackground() );
        }
        else
        {
            super.setForeground( ( unselectedForeground != null ) ? unselectedForeground : table.getForeground() );
            super.setBackground( ( unselectedBackground != null ) ? unselectedBackground : table.getBackground() );
        }

        setFont( table.getFont() );

        if ( hasFocus )
        {
            Border border = null;
            if ( isSelected )
            {
                border = UIManager.getBorder( "Table.focusSelectedCellHighlightBorder" );
            }
            if ( border == null )
            {
                border = UIManager.getBorder( "Table.focusCellHighlightBorder" );
            }
            setBorder( border );

            if ( !isSelected && table.isCellEditable( row, column ) )
            {
                Color col;
                col = UIManager.getColor( "Table.focusCellForeground" );
                if ( col != null )
                {
                    super.setForeground( col );
                }
                col = UIManager.getColor( "Table.focusCellBackground" );
                if ( col != null )
                {
                    super.setBackground( col );
                }
            }
        }
        else
        {
            setBorder( getNoFocusBorder() );
        }

        setValue( value );

        return this;
    }


    private static Border getNoFocusBorder()
    {
        if ( System.getSecurityManager() != null )
        {
            return SAFE_NO_FOCUS_BORDER;
        }
        else
        {
            return noFocusBorder;
        }
    }

}
